﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PearTunes
{
	/// <summary>
	/// Interaction logic for MusicBar.xaml
	/// </summary>
	public partial class MusicInfo : UserControl
    {

        public static DependencyProperty TrackLengthProperty =
            DependencyProperty.Register(
          "Track Length",
          typeof(TimeSpan),
          typeof(MusicInfo));

        public TimeSpan TrackLength
        {
            get
            {
                return (TimeSpan)GetValue(TrackLengthProperty);
            }
            set
            {
                SetValue(TrackLengthProperty, value);
                trackSlider.Maximum = value.TotalSeconds;
                trackSlider.Value = 0;
                Refresh();
            }
        }
        
        private TimeSpan currentPosition;
        public TimeSpan CurrentPosition
        {
            get { return currentPosition; }
            set
            {
                currentPosition = value;
                trackSlider.Value = (double)value.TotalSeconds;
            }
        }

        public string CurrentTime
        {
            get
            {
                return String.Format("{0}:{1}", currentPosition.ToString("mm"), currentPosition.ToString("ss")) ;
            }
        }

        public string RemainingTime
        {
            get { 
                TimeSpan temp = TrackLength-CurrentPosition;
                return String.Format("-{0}:{1}", temp.ToString("mm"), temp.ToString("ss"));
            }
        }

        #region Szám cím Beállítása
        public static DependencyProperty TitleProperty =
           DependencyProperty.Register(
              "Title",
              typeof(string),
              typeof(MusicInfo),
              new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeTitle)));

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        private static void ChangeTitle(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as MusicInfo).titleTextBox.Text = e.NewValue as string;
        }
        #endregion

        #region Előadó Beállítása
        public static DependencyProperty ArtistProperty =
           DependencyProperty.Register(
              "Artist",
              typeof(string),
              typeof(MusicInfo),
              new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeArtist)));

        public string Artist
        {
            get
            {
                return (string)GetValue(ArtistProperty);
            }
            set
            {
                SetValue(ArtistProperty, value);
            }
        }

        private static void ChangeArtist(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as MusicInfo).artistTextBox.Text = e.NewValue as string;
        }
        #endregion

        #region Eltelt idő beállítása
        public static DependencyProperty ElapsedTimeProperty =
           DependencyProperty.Register(
              "Elapsed Time",
              typeof(string),
              typeof(MusicInfo),
              new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeElapsedTime)));

        public string ElapsedTime
        {
            get
            {
                return (string)GetValue(ElapsedTimeProperty);
            }
            set
            {
                SetValue(ElapsedTimeProperty, value);
            }
        }

        private static void ChangeElapsedTime(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as MusicInfo).currentTime.Text = e.NewValue as string;
        }
        #endregion


        public static DependencyProperty LogoVisibleProperty =
   DependencyProperty.Register(
      "LogoVisible",
      typeof(bool),
      typeof(MusicInfo),
      new FrameworkPropertyMetadata(
        new PropertyChangedCallback(ChangeVisualStyle)));

        public bool LogoVisible
        {
            get
            {
                return (bool)GetValue(LogoVisibleProperty);
            }
            set
            {
                SetValue(LogoVisibleProperty, value);
            }
        }

        private static void ChangeVisualStyle(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == false)
            {
                (source as MusicInfo).BeginStoryboard((System.Windows.Media.Animation.Storyboard)(source as MusicInfo).FindResource("LogoToPlay"));
            }
            else
            {
                (source as MusicInfo).BeginStoryboard((System.Windows.Media.Animation.Storyboard)(source as MusicInfo).FindResource("PlayToLogo"));
            }
        }

        public MusicInfo()
		{
			this.InitializeComponent();
		}

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //currentPosition = new TimeSpan(0,0,(int)trackSlider.Value);
            Refresh();
        }

        public void Refresh()
        {
            trackSlider.Maximum = TrackLength.TotalSeconds;
            currentTime.Text = CurrentTime;
            timeLeft.Text = RemainingTime;
        }
	}
}